package com.infodms.dms.dao.publicInfoMng;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TtAsRoAddItemPO;
import com.infodms.dms.po.TtAsRoRepairPartPO;
import com.infodms.dms.po.TtAsRoRepairitemPO;
import com.infodms.dms.util.StringUtil;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

public class PublicInfoMngDao extends BaseDao{

	private PublicInfoMngDao(){}
	public static PublicInfoMngDao getInstance(){
		return new PublicInfoMngDao();
	}
	
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	//主页面第一次查询
	@SuppressWarnings("unchecked")
	public PageResult<Map<String,Object>> mainQuery(String con,int pageSize,int curPage){
		StringBuilder sql= new StringBuilder("\n");
		sql.append("select a.ctm_id,\n" );
		sql.append("       a.card_num,\n" );
		sql.append("       a.GUEST_STARS,\n" );
		sql.append("       a.ctm_name,\n" );
		sql.append("       a.main_phone,\n" );
		sql.append("       a.ctm_type,\n" );
		sql.append("       a.level_id,\n" );
		sql.append("       a.sex,\n" );
		sql.append("       to_char(c.Purchased_Date,'yyyy-MM-dd') purchased_date,\n" );
		sql.append("       c.vin,\n" );
		sql.append("       c.Yieldly,\n" );
		sql.append("       c.material_id,\n" );
		sql.append("       g.group_name as model_name,\n" );
		sql.append("       (select group_name\n" );
		sql.append("          from tm_vhcl_material_group\n" );
		sql.append("         where group_id = g.parent_group_id) as series_name\n" );
		sql.append("  from tt_customer              a,\n" );
		sql.append("       TT_DEALER_ACTUAL_SALES   b,\n" );
		sql.append("       tm_vehicle               c,\n" );
		sql.append("       tm_vhcl_material_group   g,\n" );
		sql.append("       tm_vhcl_material_group_r r\n" );
		sql.append(" where a.ctm_id = b.ctm_id\n" );
		sql.append("   and b.Vehicle_Id = c.vehicle_id\n" );
		sql.append("   and c.material_id = r.material_id\n" );
		sql.append("   and g.group_id = (select parent_group_id\n" );
		sql.append("                       from tm_vhcl_material_group\n" );
		sql.append("                      where group_id = r.group_id)\n");
		if(StringUtil.notNull(con))
			sql.append(con);
		//sql.append(" order by a. card_num\n");
		return this.pageQuery(sql.toString(),null,this.getFunName(), pageSize, curPage) ;
	}

	//根据VIN查询维修配件信息
	@SuppressWarnings("unchecked")
	public List<TtAsRoRepairPartPO> queryParts(String vin){
		StringBuilder sql= new StringBuilder("\n");
		sql.append("select *\n" );
		sql.append("  from tt_as_ro_repair_part\n" );
		sql.append(" where ro_id in\n" );
		sql.append("       (select id from tt_as_wr_application where vin = '").append(vin).append("')");
		return this.select(TtAsRoRepairPartPO.class, sql.toString(), null);
	}
	
	//根据VIN查询维修项目
	@SuppressWarnings("unchecked")
	public List<TtAsRoRepairitemPO> queryItem(String vin){
		StringBuilder sql= new StringBuilder("\n");
		sql.append("select *\n" );
		sql.append("  from tt_as_ro_repairitem\n" );
		sql.append(" where ro_no in\n" );
		sql.append("       (select vin from tt_as_wr_application where vin = '").append(vin).append("')");
		return this.select(TtAsRoRepairitemPO.class, sql.toString(),null);
	}
	
	//根据VIN查询附加项目
	@SuppressWarnings("unchecked")
	public List<TtAsRoAddItemPO> queryAddItem(String vin){
		StringBuilder sql= new StringBuilder("\n");
		sql.append("select *\n" );
		sql.append("  from tt_as_ro_add_item\n" );
		sql.append(" where ro_id in\n" );
		sql.append("       (select id from tt_as_wr_application where vin = '").append(vin).append("')");
		return this.select(TtAsRoAddItemPO.class, sql.toString(), null);
	}
}
